package com.atguigu;

import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import com.alibaba.ververica.cdc.debezium.StringDebeziumDeserializationSchema;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class FLinkCDC {
    public static void main(String[] args) throws Exception {
        //1.创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //1.1启CK并制定后端FS memory fs rocksdb
        env.setStateBackend(new FsStateBackend("hdfs://node1:8020/gmall-flink/ck"));
        //5秒触发
        env.enableCheckpointing(5000L);
        //
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointTimeout(10000L);
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(2);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(3000);


        /*2.Flink-CDC 将读取 binlog 的位置信息以状态的方式保存在 CK,
        如果想要做到断点续传,需要从 Checkpoint 或者 Savepoint 启动程序
        */
        DebeziumSourceFunction<String> mysqlSource = MySQLSource.<String>builder()
                .hostname("node1")
                .port(3306)
                .username("root")
                .password("123456")
                .databaseList("goods")
                .tableList("goods.base_trademark")
                .startupOptions(StartupOptions.initial())
                .deserializer(new StringDebeziumDeserializationSchema())
                .build();
        DataStreamSource<String> mysqlDS = env.addSource(mysqlSource);
        mysqlDS.print();
        env.execute("FLinkCDC");
    }
}
